<template>
  <Model ref="modelRef" :title="title" @Confrm="handleConfirm">
    <view class="" v-if="goodsDetail">
      <swiper v-if="goodsDetail.pic" class="swiper" circular indicator-dots>
        <swiper-item v-for="item in goodsDetail.pic" :key="item">
          <image :src="item" mode="widthFix"></image>
        </swiper-item>
      </swiper>

      <view class="p_name">
        <view class="img"></view>
        <view class="">
          <view class="">{{ goodsDetail.productName }}</view>
          <view class="good-year">{{ goodsDetail.year }}</view>
        </view>
      </view>

      <view class="goods-num">
        <view class="goods-sum" @click="decreaseNum">-</view>
        <view class="input">{{ num }}</view>
        <view class="goods-sum" @click="increaseNum">+</view>
      </view>
    </view>
  </Model>
</template>

<script setup>
import { ref } from "vue";
import Model from "@/components/Model/Model.vue";

const props = defineProps({
  title: {
    type: String,
    default: "",
  },
  goodsDetail: {
    type: Object,
    default: () => null,
  },
});

const emit = defineEmits(["confirm"]);
const modelRef = ref(null);
const num = ref(1);

const decreaseNum = () => {
  if (num.value > 1) {
    num.value--;
  }
};

const increaseNum = () => {
  if (num.value < props.goodsDetail?.stock) {
    num.value++;
  }
};

const handleConfirm = () => {
  emit("confirm", {
    num: num.value,
    goodsDetail: props.goodsDetail,
  });
};

// 对外暴露方法
defineExpose({
  open: () => modelRef.value?.control(true),
  close: () => modelRef.value?.control(false),
});
</script>

<style lang="scss" scoped>
.swiper {
  width: 100%;
  height: 300px;
  image {
    width: 100%;
    height: 300px;
  }
}

.p_name {
  padding: 10px;
  display: flex;
  justify-content: center;
  gap: 10px;

  .good-year {
    margin-top: 5px;
    color: #666;
    font-size: 14px;
  }
}

.goods-num {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 10px;

  .goods-sum {
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #eee;
    cursor: pointer;
  }

  .input {
    min-width: 40px;
    text-align: center;
    margin: 0 10px;
  }
}
</style>
